var grid;
function initGrid(){
	grid = $("#maingrid").ligerGrid({
		url:HTR.U('index/Ads/pageQuery'),
		pageSize:HTR.pageSize,
		pageSizeOptions:HTR.pageSizeOptions,
		height:'99%',
        width:'100%',
        minColToggle:6,
        rownumbers:true,
        columns: [
	        { display: '标题', name: 'ad_name', isSort: false},
	        { display: '广告位置', name: 'ad_position', isSort: false,render:function(rowdata, rowindex, value){
	        	return value==0?'头部轮播':'底部推荐';
	        }},
	        { display: '广告开始日期', name: 'start_date', isSort: false},
	        { display: '广告结束日期', name: 'end_date', isSort: false},
	        { display: '图标', name: 'ad_pic', height: '300', isSort: false,render:function(rowdata, rowindex, value){
              return'<img src="'+HTR.conf.ROOT+'/'+value+'" height="28px" />';
	        }},
	        { display: '排序号', name: 'ad_sort', isSort: false,render:function(rowdata, rowindex, value){
              return '<span style="cursor:pointer;" ondblclick="changeSort(this,'+rowdata["ad_id"]+');">'+value+'</span>';
          }},
	        { display: '操作', name: 'op',isSort: false,render: function (rowdata, rowindex, value){
	        	var h = "";
	            if(HTR.GRANT.GGGL_02)h += "<a href='"+HTR.U('index/Ads/toEdit','id='+rowdata['ad_id'])+"'>修改</a> ";
	            if(HTR.GRANT.GGGL_03)h += "<a href='javascript:toDel(" + rowdata['ad_id'] + ")'>删除</a> "; 
	            return h;
	        }}
        ]
    });
}
function toDel(id){
	var box = HTR.confirm({content:"您确定要删除该记录吗?",yes:function(){
    var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
  	$.post(HTR.U('index/Ads/del'),{id:id},function(data,textStatus){
		  layer.close(loading);
		  var json = HTR.toIndexJson(data);
		  if(json.status=='1'){
		    HTR.msg("操作成功",{icon:1});
		    layer.close(box);
	        grid.reload();
		  }else{
		    HTR.msg(json.msg,{icon:2});
		  }
	 });
  }});
}

var oldSort;
function changeSort(t,id){
  $(t).attr('ondblclick'," ");
  var html = "<input type='text' id='sort-"+id+"' style='width:30px;' onblur='doneChange(this,"+id+")' value='"+$(t).html()+"' />";
  $(t).html(html);
  $('#sort-'+id).focus();
  $('#sort-'+id).select();
  oldSort = $(t).html();
}
function doneChange(t,id){
  var sort = ($(t).val()=='')?0:$(t).val();
  if(sort==oldSort){
    $(t).parent().attr('ondblclick','changeSort(this,'+id+')');
    $(t).parent().html(parseInt(sort));
    return;
  }
  $.post(HTR.U('index/ads/changeSort'),{id:id,ad_sort:sort},function(data){
    var json = HTR.toIndexJson(data);
    if(json.status==1){
      $(t).parent().attr('ondblclick','changeSort(this,'+id+')');
      $(t).parent().html(parseInt(sort));
    }
  });
}


		
//查询
function adsQuery(){
	var query = HTR.getParams('.query');
  grid.set('url',HTR.U('index/ads/pageQuery',query));
}

function editInit(){
  //文件上传
	HTR.upload({
	  pick:'#adFilePicker',
	  formData: {dir:'adspic'},
    compress:false,//默认不对图片进行压缩
	  accept: {extensions: 'gif,jpg,jpeg,bmp,png',mimeTypes: 'image/*'},
	  callback:function(f){
		  var json = HTR.toIndexJson(f);
		  if(json.status==1){
			$('#uploadMsg').empty().hide();
      var html = '<img src="'+HTR.conf.ROOT+'/'+json.savePath+json.thumb+'" />';
      $('#preview').html(html);
      // 图片路径
      $('#ad_pic').val(json.savePath+json.thumb);
		  }
	  },
	  progress:function(rate){
	      $('#uploadMsg').show().html('已上传'+rate+"%");
	  }
  });
  

 /* 表单验证 */
  $('#adsForm').validator({
    fields: {
      ad_name: {
        rule:"required;",
        msg:{required:"广告标题不能为空"},
        tip:"请输入广告标题",
        ok:"验证通过",
      },
      ad_pic: {
        rule:"required;",
        msg:{required:"请上传广告图片"},
        tip:"请上传广告图片",
        ok:"",
      },
      start_date: {
        rule:"required;match(lt, end_date, date)",
        msg:{required:"请选择广告开始时间",match:"必须小于广告结束时间"},
        ok:"验证通过",
      },
      end_date: {
        rule:"required;match(gt, start_date, date)",
        msg:{required:"请选择广告结束时间",match:"必须大于广告开始时间"},
        ok:"验证通过",
      }
    },
    valid: function(form){
      var params = HTR.getParams('.ipt');
      var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
      $.post(HTR.U('index/Ads/'+((params.ad_id==0)?"add":"edit")),params,function(data,textStatus){
        layer.close(loading);
        var json = HTR.toIndexJson(data);
        if(json.status=='1'){
          HTR.msg("操作成功",{icon:1});
          location.href=HTR.U('Index/Ads/index');
        }else{
          HTR.msg(json.msg,{icon:2});
        }
      });
    }
  });
}